﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyVoteMVC.Domain;
using MyVoteMVC.BackstageManagement.Controllers;

namespace MyVoteMVC.BackstageManagement.Views
{
    public interface IWeeklylotteryView
    {
        void GetActivityListComplete(IList<VT_Activities> list);
        void GetActivityUsersComplete(IList<VT_Users> list);
        void GetWeekLotteriesComplete(IList<VT_Lotteries> list);
        void AddWeekLotteriesComplete(bool bl);
        void DeleteLotteriesComplete(bool bl);
    }

    public partial class Weeklylottery : Form, IWeeklylotteryView
    {
        private IWeeklyLotteryControl controller;
        public Weeklylottery()
        {
            InitializeComponent();
            controller = new WeeklyLotteryControl(this);
        }

        private void Weeklylottery_Load(object sender, EventArgs e)
        {
            controller.GetActivityList();
        }
       
        public void GetActivityListComplete(IList<VT_Activities> list)
        {
            gvActivity.DataSource = list;
            if (gvActivity.DataSource != null)
            {
                for (int i = 0; i < gvActivity.ColumnCount; i++)
                {
                    gvActivity.Columns[i].Visible = false;
                }
                gvActivity.Columns["Activity_ID"].Visible = true;
                gvActivity.Columns["Activity_ID"].HeaderText = "项目编号";
                gvActivity.Columns["Activity_Title"].Visible = true;
                gvActivity.Columns["Activity_Title"].HeaderText = "项目标题";
            }
        }

        public void GetActivityUsersComplete(IList<VT_Users> list)
        {
            gvUserActivity.DataSource = null;
            gvUserActivity.DataSource = list;
            if (gvUserActivity.DataSource != null)
            {
                for (int i = 0; i < gvUserActivity.ColumnCount; i++)
                {
                    gvUserActivity.Columns[i].Visible = false;
                }
                gvUserActivity.Columns["User_RealName"].Visible = true;
                gvUserActivity.Columns["User_RealName"].HeaderText = "真实姓名";
                gvUserActivity.Columns["User_Email"].Visible = true;
                gvUserActivity.Columns["User_Email"].HeaderText = "用户邮箱";
                gvUserActivity.Columns["User_Name"].Visible = true;
                gvUserActivity.Columns["User_Name"].HeaderText = "用户名";

                groupBox5.Text = "参加活动的用户(没中奖的用户" + gvUserActivity.RowCount + "位)";
            }
            else
            {
                groupBox5.Text = "参加活动的用户(没中奖的用户)";
            }
        }
        public void GetWeekLotteriesComplete(IList<VT_Lotteries> list)
        {
            gvlotteryUser.DataSource = null;
            gvlotteryUser.DataSource = list;
            if (gvlotteryUser.DataSource != null)
            {
                for (int i = 0; i < gvlotteryUser.ColumnCount; i++)
                {
                    gvlotteryUser.Columns[i].Visible = false;
                }
                gvlotteryUser.Columns["User_Email"].Visible = true;
                gvlotteryUser.Columns["User_Email"].HeaderText = "中奖用户邮箱";
                gvlotteryUser.Columns["Created_Date"].Visible = true;
                gvlotteryUser.Columns["Created_Date"].HeaderText = "中奖时间";
                groupBox3.Text = "活动中奖用户" + gvlotteryUser.RowCount + "位";
            }
            else
            {
                groupBox3.Text = "活动中奖用户";
            }
        }
        private void gvActivity_SelectionChanged(object sender, EventArgs e)
       {
           if (gvActivity.SelectedRows.Count > 0 && gvActivity.SelectedRows[0].DataBoundItem != null)
           {
               VT_Activities v_Activitise = gvActivity.SelectedRows[0].DataBoundItem as VT_Activities;
               controller.GetActivityUsersList(v_Activitise.Activity_ID);
               controller.GetWeekLotteriesList(v_Activitise.Activity_ID);
               lblActivityName.Text = v_Activitise.Activity_Title;
               lblActivityID.Text = v_Activitise.Activity_ID;
           }
           else
           {
               lblActivityID.Text = "";
               lblActivityName.Text = "";
           }
        }

        private void btnLottery_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(lblActivityName.Text.Trim()) || string.IsNullOrEmpty(lblActivityID.Text.Trim()))
                {
                    MessageBox.Show("请选中一个活动再抽奖！");
                    return;
                }
                if (gvUserActivity.RowCount <= Int32.Parse(txtNumbers.Text.Trim()))
                {
                    if (MessageBox.Show("待抽奖用户不大于中奖人数，是否继续", "系统提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                        return;
                }
                if (btnLottery.Text == "开始抽奖")
                {
                    btnLottery.Enabled = false;
                }
                LotteryProgram(gvUserActivity, Int32.Parse(txtNumbers.Text.Trim()));
                //刷新没有中奖和中奖的列表
                gvActivity_SelectionChanged(null, null);
                btnLottery.Enabled = true;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public void AddWeekLotteriesComplete(bool bl)
        {
            return  ;
        }
        public void DeleteLotteriesComplete(bool bl)
        {
            return;
        }
      
        private void LotteryProgram(DataGridView dv, int number)
        {
            if (dv == null)
            {
                MessageBox.Show("没有数据！");
                return;
            }
            //假如待抽奖用户不大于中奖人数，则全部中
            if (dv.RowCount <= number)
            {
                txtResult.Text = "中奖用户为以下：";
                for (int i = 0; i < dv.RowCount; i++)
                {
                    txtResult.Text += "\r\n" + "用户名：" + dv.Rows[i].Cells["User_Name"].Value.ToString();
                    //插入中奖表记录
                   controller.AddWeekLotteries(lblActivityID.Text.Trim(), dv.Rows[i].Cells["User_ID"].Value.ToString(), dv.Rows[i].Cells["User_Email"].Value.ToString());
                }
            }
            else
            {
                int[] numberlist = new int[number];
                for (int i = 0; i < number; i++)
                {
                    numberlist[i] = -1;
                }
                for (int i = 0; i < number; i++)
                {
                    Random rnd = new Random();
                    int rndNum = rnd.Next(0, dv.RowCount - 1);
                    bool isRepeat = false;
                    for (int j = 0; j < numberlist.Length;j++ )
                    {
                        if (numberlist[j] == rndNum)
                        {
                            isRepeat = true;
                            break;
                        }
                    }
                    if (isRepeat == true)
                    {
                        //重复则重新再抽查一次
                        i = i - 1;
                    }
                    else numberlist[i] = rndNum;
                }

                //插入中奖表记录 
                txtResult.Text = "中奖用户为以下：";
                for (int i = 0; i < numberlist.Length; i++)
                {
                    //插入中奖表记录 
                    txtResult.Text += "\r\n" + "用户名：" + dv.Rows[numberlist[i]].Cells["User_Name"].Value.ToString();
                    controller.AddWeekLotteries(lblActivityID.Text.Trim(), dv.Rows[numberlist[i]].Cells["User_ID"].Value.ToString(), dv.Rows[numberlist[i]].Cells["User_Email"].Value.ToString());    
                }
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (gvlotteryUser.SelectedRows.Count > 0)
            {
                btnDel.Enabled = false;
                for (int i = 0; i < gvlotteryUser.SelectedRows.Count; i++)
                {
                    VT_Lotteries v_Lotteries = gvlotteryUser.SelectedRows[i].DataBoundItem as VT_Lotteries;
                    controller.DeleteLotteries(v_Lotteries.Lottery_ID);
                }
                gvActivity_SelectionChanged(null, null);
                MessageBox.Show("删除成功");
                btnDel.Enabled = true;
            }
            else
            {
                MessageBox.Show("请选中要删除的数据！");
            }
            
        }
    }
}
